﻿using System;
using System.Threading;
using System.Threading.Tasks;
using MagistrAPI.Enums;
using MagistrWPFUI.Presenter;
using System.Threading.Tasks;

namespace MagistrWPFUI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        private ModelSupplier _presenter;

        public MainWindow()
        {
            InitializeComponent();
            _presenter = new ModelSupplier(zgc);
            foreach (var characteristic in _presenter.Characteristicses)
            {
                Characteristic.Items.Add(characteristic);
            }
        }

        private void IntegrateClick(object sender, System.Windows.RoutedEventArgs e)
        {
            double x = string.IsNullOrEmpty(X.Text) ? -3608998.7560643037 : double.Parse(X.Text);
            double y = string.IsNullOrEmpty(Y.Text) ? 2027424.9206698083 : double.Parse(Y.Text);
            double z = string.IsNullOrEmpty(Z.Text) ? 4916970.4170327466 : double.Parse(Z.Text);
            double vX = string.IsNullOrEmpty(Vx.Text) ? -959.5669302239 : double.Parse(Vx.Text);
            double vY = string.IsNullOrEmpty(Vy.Text) ? -400.9768981914 : double.Parse(Vy.Text);
            double vZ = string.IsNullOrEmpty(Vz.Text) ? 1440.0052077401 : double.Parse(Vz.Text);
            double wX = string.IsNullOrEmpty(Wx.Text) ? 0 : double.Parse(Wx.Text);
            double wY = string.IsNullOrEmpty(Wy.Text) ? 0 : double.Parse(Wy.Text);
            double wZ = string.IsNullOrEmpty(Wz.Text) ? 0 : double.Parse(Wz.Text);
            double fi = string.IsNullOrEmpty(Al.Text) ? 0 : double.Parse(Al.Text);
            double al = string.IsNullOrEmpty(Fi.Text) ? 0.872664626 : double.Parse(Fi.Text);
            double gam = string.IsNullOrEmpty(Gam.Text) ? 0 : double.Parse(Gam.Text);
            try
            {
                pb.IsIndeterminate = true;
                Task integrateTask = Task.Factory.StartNew(() =>
                                                           _presenter.Integrate(new[]
                                                                                    {
                                                                                        x, y, z, vX, vY, vZ, wX, wY, wZ,
                                                                                        al
                                                                                        , fi, gam
                                                                                    }));
                var ui = TaskScheduler.FromCurrentSynchronizationContext();
                integrateTask.ContinueWith(result =>
                                               {
                                                   pb.IsIndeterminate = false;
                                               }, ui);

            }
            catch (Exception ex)
            {

            }

        }

        private void DrawGraphicClick(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                _presenter.DrawGraphic((Characteristics)Characteristic.SelectionBoxItem);

            }
            catch (Exception ex)
            {
                
            }
        }
    }
}
